package cn.pconline.autoclub.service;

import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubDraft;
import cn.pconline.autoclub.util.RegionUtil;
import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;

public class ClubDraftService {

    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private GeliDao geliDao;

    /**
     * 根据用户id获取创建车友会草稿
     *
     * @param userId
     * @return
     */
    public ClubDraft findDraftByUserId(long userId) {
        if (userId <= 0) {
            return null;
        }
        try {
            return geliDao.find(ClubDraft.class, userId);
        } catch (EmptyResultDataAccessException e) {
            return null;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 创建草稿（存在的进行更新）
     *
     * @param clubDraft
     * @return
     */
    public int createOrUpdate(ClubDraft clubDraft) {
        if (clubDraft == null || clubDraft.getUserId() <= 0) {
            return 0;
        }
        String sql = getCreateOrUpdateSql(clubDraft);
        int result = geliDao.getNamedParameterJdbcTemplate().update(sql, geliOrm.map(clubDraft));
        geliDao.deleteCache(ClubDraft.class, clubDraft.getUserId());
        return result;
    }

    public String getCreateOrUpdateSql(Object entity) {
        String sql = geliOrm.getCreateSql(entity);
        return sql.replaceFirst("insert", "replace");
    }
	
    public Club parseClubDraftToClub(ClubDraft clubDraft){
        if(clubDraft==null){
            return null;
        }
        Club club = new Club();
        club.setAdminId(clubDraft.getUserId());
        club.setBrandId(clubDraft.getBrandId());
        club.setBrandName(clubDraft.getBrandName());
        club.setCityId(clubDraft.getCityId());
        club.setCityName(clubDraft.getCityName());
        club.setClubUrl(clubDraft.getClubUrl());
        club.setCreateAt(clubDraft.getCreateAt());
        club.setCreateBy(clubDraft.getCreateBy());
        club.setIntroduce(clubDraft.getIntroduce());
        club.setName(clubDraft.getName());
        club.setProvinceId(clubDraft.getProvinceId());
        club.setProvinceName(clubDraft.getProvinceName());
        club.setQqGroup(clubDraft.getQqGroup());
        long regionId = RegionUtil.getRegionId(clubDraft.getProvinceId());
        club.setRegionId(regionId);
        club.setRegionName(RegionUtil.getRegionName(regionId));
        club.setSeriesId(clubDraft.getSeriesId());
        club.setSeriesName(clubDraft.getSeriesName());
        club.setType(clubDraft.getTypeId());
        club.setThemeId(clubDraft.getThemeId());
        club.setUpdateAt(clubDraft.getUpdateAt());
        club.setUpdateBy(clubDraft.getUpdateBy());
        club.setLevelId(0);
        club.setLogo("");
        club.setMemberNum(0);
        club.setReplyNum(0);
        club.setStatus(Club.STATUS_UNAUDITED);
        club.setTopicNum(0);
        club.setVipNum(0);
        return club;
    }
    public ClubDraft parseClubToClubDraft(Club club){
        if(club==null){
            return null;
        }
        ClubDraft clubDraft = new ClubDraft();
        clubDraft.setUserId(club.getAdminId());
        clubDraft.setBrandId(club.getBrandId());
        clubDraft.setBrandName(club.getBrandName());
        clubDraft.setCityId(club.getCityId());
        clubDraft.setCityName(club.getCityName());
        clubDraft.setClubUrl(club.getClubUrl());
        clubDraft.setCreateAt(club.getCreateAt());
        clubDraft.setCreateBy(club.getCreateBy());
        clubDraft.setIntroduce(club.getIntroduce());
        clubDraft.setName(club.getName());
        clubDraft.setProvinceId(club.getProvinceId());
        clubDraft.setProvinceName(club.getProvinceName());
        clubDraft.setQqGroup(club.getQqGroup());
        clubDraft.setSeriesId(club.getSeriesId());
        clubDraft.setSeriesName(club.getSeriesName());
        clubDraft.setTypeId(club.getType());
        clubDraft.setThemeId(club.getThemeId());
        clubDraft.setUpdateAt(club.getUpdateAt());
        clubDraft.setUpdateBy(club.getUpdateBy());
        return clubDraft;
    }
    public void deleteClubDraft(ClubDraft clubDraft){
        geliDao.delete(clubDraft,clubDraft.getUserId());
    }
}
